package Eyjafjalla.panel;

import Eyjafjalla.character.Eyjafjalla;
import Eyjafjalla.effect.RefreshCloudEnergyEffect;
import Eyjafjalla.helper.StringHelper;
import Eyjafjalla.orb.CloudEnergyOrb;
import Eyjafjalla.power.AdelePower;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Interpolation;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.FontHelper;
import com.megacrit.cardcrawl.helpers.Hitbox;
import com.megacrit.cardcrawl.helpers.MathHelper;
import com.megacrit.cardcrawl.helpers.TipHelper;
import com.megacrit.cardcrawl.localization.UIStrings;
import com.megacrit.cardcrawl.powers.AbstractPower;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import com.megacrit.cardcrawl.ui.panels.AbstractPanel;

public class CloudEnergyPanel extends AbstractPanel {
    private static final UIStrings uiStrings;
    private Hitbox tipHitbox;
    private Texture gainEnergyImg;
    private float energyVfxAngle;
    private float energyVfxScale;
    private Color energyVfxColor;
    public static float fontScale;
    public static float energyVfxTimer;
    private static final Color ENERGY_TEXT_COLOR;
    public static int totalCount;
    public CloudEnergyOrb cloudEnergyOrb;

    public static final float CEX = 218F* Settings.xScale;
    public static final float CEY = 390F*Settings.yScale;

    public CloudEnergyPanel(){
        super(CEX,CEY,-460.0F * Settings.scale, 400.0F * Settings.yScale,12.0F * Settings.scale, -12.0F * Settings.scale, (Texture)null, true);
        //0.9 F
        this.tipHitbox = new Hitbox(0F,0F,108F*Settings.scale,108F*Settings.scale);
        this.energyVfxAngle = 0.0F;
        this.energyVfxScale = Settings.scale;
        this.energyVfxColor = Color.WHITE.cpy();
        this.gainEnergyImg = AbstractDungeon.player.getEnergyImage();
        this.cloudEnergyOrb = new CloudEnergyOrb(Eyjafjalla.orbTextures,Eyjafjalla.VFX,Eyjafjalla.LAYER_SPEED);
    }

    public static void setEnergy(int energy){
        totalCount = energy;
        AbstractDungeon.effectsQueue.add(new RefreshCloudEnergyEffect());
        energyVfxTimer = 2.0F;
        fontScale = 2.0F;
    }

    public static void addEnergy(int e){
        totalCount += e;
        if (totalCount > 999) {
            totalCount = 999;
        }
        AbstractDungeon.effectsQueue.add(new RefreshCloudEnergyEffect());
        fontScale = 2.0F;
        energyVfxTimer = 2.0F;
    }

    public static void useEnergy(int e){
        totalCount -= e;
        int times = e;
        if(totalCount<0){
            times += totalCount;
            totalCount =0;
        }
//        if(times>0){
//            AbstractPower p = AbstractDungeon.player.getPower(AdelePower.POWER_ID);
//            if(p!=null){
//                for(int i =0;i<times;i++)
//                    p.onSpecificTrigger();
//            }
//        }
    }

    public void update(){
        cloudEnergyOrb.updateOrb(totalCount);
        this.updateVfx();
        if(fontScale!=1f){
            fontScale = MathHelper.scaleLerpSnap(fontScale,1F);
        }
        this.tipHitbox.update();
        if(this.tipHitbox.hovered&&!AbstractDungeon.isScreenUp){
            AbstractDungeon.overlayMenu.hoveredTip = true;
        }

    }

    private void updateVfx(){
        if (energyVfxTimer != 0.0F) {
            this.energyVfxColor.a = Interpolation.exp10In.apply(0.5F, 0.0F, 1.0F - energyVfxTimer / 2.0F);
            this.energyVfxAngle += Gdx.graphics.getDeltaTime() * -30.0F;
            this.energyVfxScale = Settings.scale * Interpolation.exp10In.apply(1.0F, 0.1F, 1.0F - energyVfxTimer / 2.0F);
            energyVfxTimer -= Gdx.graphics.getDeltaTime();
            if (energyVfxTimer < 0.0F) {
                energyVfxTimer = 0.0F;
                this.energyVfxColor.a = 0.0F;
            }
        }
    }

    @Override
    public void render(SpriteBatch sb) {
        this.tipHitbox.move(this.current_x,this.current_y);
        this.renderOrb(sb);
        this.renderVfx(sb);
        String energyMsg = String.valueOf(totalCount);
        //todo
        AbstractDungeon.player.getEnergyNumFont().getData().setScale(fontScale);
        FontHelper.renderFontCentered(sb,AbstractDungeon.player.getEnergyNumFont(),energyMsg,this.current_x,this.current_y,ENERGY_TEXT_COLOR);
        this.tipHitbox.render(sb);
        if (this.tipHitbox.hovered && AbstractDungeon.getCurrRoom().phase == AbstractRoom.RoomPhase.COMBAT && !AbstractDungeon.isScreenUp) {
            TipHelper.renderGenericTip(70.0F * Settings.scale, 580.0F * Settings.scale, uiStrings.TEXT[0], uiStrings.TEXT[1]);
        }
    }

    private void renderOrb(SpriteBatch sb){
        if(totalCount==0){
            cloudEnergyOrb.renderOrb(sb,false,this.current_x,this.current_y);
        }
        else {
            cloudEnergyOrb.renderOrb(sb,true,this.current_x,this.current_y);
        }
    }

    private void renderVfx(SpriteBatch sb){
        if(energyVfxTimer!=0F){
            sb.setBlendFunction(770, 1);
            sb.setColor(this.energyVfxColor);
            sb.draw(this.gainEnergyImg, this.current_x - 128.0F, this.current_y - 128.0F, 128.0F, 128.0F, 256.0F, 256.0F, this.energyVfxScale, this.energyVfxScale, -this.energyVfxAngle + 50.0F, 0, 0, 256, 256, true, false);
            sb.draw(this.gainEnergyImg, this.current_x - 128.0F, this.current_y - 128.0F, 128.0F, 128.0F, 256.0F, 256.0F, this.energyVfxScale, this.energyVfxScale, this.energyVfxAngle, 0, 0, 256, 256, false, false);
            sb.setBlendFunction(770, 771);
        }
    }

    public static int getCurrentEnergy(){
        return AbstractDungeon.player==null?0:totalCount;
    }

    static {
        uiStrings = CardCrawlGame.languagePack.getUIString(StringHelper.CLOUD_ENERGY);
        ENERGY_TEXT_COLOR = Color.GOLD.cpy();
        fontScale = 1.0F;
        totalCount = 0;
        energyVfxTimer = 0.0F;
    }
}
